﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Demo.FolderOp.ImgManager
{
    using System.Drawing;
    
    public class ImgMethod
    {
        private static Bitmap myImg1;
        private static Bitmap myImg2;
        private static bool IsSimilar(Bitmap image1, Bitmap image2)
        {
            try
            {
                // 提取图像特征  
                int width1 = image1.Width;
                int height1 = image1.Height;
                int width2 = image2.Width;
                int height2 = image2.Height;

                if (width1 != width2 || height1 != height2)
                {
                    throw new ArgumentException("Images must be the same size.");
                }
                // 计算像素相似度  
                int similarity = 0;
                int loop = 50;
                int randheight1 = 0;
                int randwidth1 = 0;
                Random randomHeight = new Random(height1);
                Random randomWidth = new Random(width1);
                do
                {
                    randheight1 = randomHeight.Next(0, height1 - 1);
                    randwidth1 = randomWidth.Next(0, width1 - 1);

                    Color color1 = image1.GetPixel(randwidth1, randheight1);
                    Color color2 = image2.GetPixel(randwidth1, randheight1);
                    similarity += (color1 == color2) ? 1 : 0;
                    loop--;
                } while (loop > 0);

                if (similarity >= 40)
                {
                    return true;
                }
                return false;

                //for (int y = 0; y < height1; y++)
                //{
                //    for (int x = 0; x < width1; x++)
                //    {
                //        Color color1 = image1.GetPixel(x, y);
                //        Color color2 = image2.GetPixel(x, y);
                //        similarity += (color1 == color2) ? 1 : 0;
                //        if (similarity >= 100) {
                //            isRight = true;
                //            break;
                //        }
                //    }
                //    if (isRight) {
                //        break;
                //    }
                //}
                //if (isRight)
                //{
                //    return true;
                //}

                // 计算相似度百分比  
                //double similarityPercentage = (double)similarity / (width1 * height1);

                // 设置相似度阈值  
                //double threshold = 0.80; // 可根据需求调整阈值大小  

                // 判断相似度是否超过阈值  
                //return similarityPercentage > threshold;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool CheckImgSimilar(string oldUrl, string newUrl)
        {
            try
            {
                myImg1 = new Bitmap(oldUrl);
                myImg2 = new Bitmap(newUrl);
                return IsSimilar(myImg1, myImg2);
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}


